package com.chesnutconsulting.reports;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;

public class FileTraversal {
	public static final void traverse(final File f, ReportCategory category)
			throws IOException {
		// System.out.println(category.toString());

		if (f.isDirectory()) {
			ReportCategory reportCategory = new ReportCategory(f.getName()
					+ " Category", category.getCategoryId() + "-" + f.getName());
			category.addCategory(reportCategory);
			onDirectory(f, reportCategory);
			final File[] childs = f.listFiles();/*
												 * new FilenameFilter() {
												 * 
												 * @Override public boolean
												 * accept(File file, String
												 * name) {
												 * 
												 * (StringUtils.defaultString
												 * (name).toLowerCase()
												 * .endsWith("jrxml")) { return
												 * true; } else { return false;
												 * }
												 * 
												 * } }
												 * 
												 * );
												 */
			for (File child : childs) {
				traverse(child, reportCategory);
			}
			ReportsInitializer.getRootReportCategory().toString();
			return;
		}
		Report lReport = new Report(f.getName() + " Report",
				f.getAbsolutePath(), f.getName(), category.getCategoryId()
						+ "-" + f.getName());
		category.addReport(lReport);

		onFile(f, lReport);
	}

	public static void onDirectory(final File d, ReportCategory reportCategory) {
	}

	public static void onFile(final File f, Report report) {
		String reportName = report.getFileName();
		String reportAbsolutePath = report.getAbsolutePath();

		System.out.println("++++++" + report.getReportId());

		JasperReport jasperReport = null;
		try {
			jasperReport = JasperCompileManager
					.compileReport(reportAbsolutePath);
		} catch (JRException e) {
			System.out.println( "Problem with report file [" + reportAbsolutePath + "], [" + e.getMessage() + "]. File may be invalid.");
			return;
		}

		// Add file name without extension as key
		String reportNameKey = reportName.substring(0,
				reportName.lastIndexOf('.'));

		ReportsInitializer.getReportWidgetTypeMap().put(reportNameKey,
				new ArrayList<UserInterfaceElement>());

		JRParameter[] parameters = jasperReport.getParameters();
		for (int j = 0; j < parameters.length; j++) {
			JRParameter parameter = parameters[j];

			JRPropertiesMap propsMap = parameter.getPropertiesMap();

			if (propsMap != null) {

				String uiElementValue = propsMap.getProperty("ui-element");
				String uiElementTypeValue = propsMap
						.getProperty("ui-element-type");
				String uiElementSequenceValue = propsMap
						.getProperty("ui-element-sequence");
				String uiElementCssClassValue = propsMap
						.getProperty("ui-element-css-class");

				if (uiElementValue != null && uiElementTypeValue != null
						&& uiElementSequenceValue != null
						&& uiElementCssClassValue != null
						&& uiElementValue.equalsIgnoreCase("true")) {

					String cssClass = propsMap
							.getProperty("ui-element-css-class");
					String htmlElementType = propsMap
							.getProperty("ui-element-type");
					String htmlElementLabel = propsMap
							.getProperty("ui-element-label");
					String htmlElementName = parameter.getName();
					int htmlElementSequence = Integer.valueOf(propsMap
							.getProperty("ui-element-sequence"));

					UserInterfaceElement uiElement = null;
					try {
						uiElement = new UserInterfaceElement(cssClass,
								htmlElementType, htmlElementLabel,
								htmlElementName, htmlElementSequence);
						report.addUiElement(uiElement);
					} catch (Exception e) {
						e.printStackTrace();
					}
					ReportsInitializer.getReportWidgetTypeMap().get(reportNameKey).add(uiElement);
				}
			}
		}
	}
}
